package com.hsource.doctorcenter.manager.factory.callBackService;

import com.alibaba.fastjson.JSONObject;
import com.hsource.common.utils.DateUtil;
import com.hsource.common.utils.bean.BeanUtils;
import com.hsource.doctorcenter.conf.dataSource.aspect.SnowflakeIdWorker;
import com.hsource.doctorcenter.domain.order.Order;
import com.hsource.doctorcenter.domain.order.OrderDoctor;
import com.hsource.doctorcenter.feign.PaymentFeignClient;
import com.hsource.doctorcenter.service.api.IApiService;
import com.hsource.doctorcenter.service.order.IOrderDoctorService;
import com.hsource.doctorcenter.service.order.IOrderService;
import com.hsource.doctorcenter.util.SpringUtils;
import com.hsource.doctorcenter.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author yangYang
 * @version 1.0
 * @date 2020/10/17 21:14
 * @describe 快捷电话回调服务处理
 */
@Slf4j
public class FastPhoneCallBackService {

    /**
     * 回调数据解析、处理
     * @param callBackDataMap 回调数据
     */
    public void callBackDataService(Map<String,Object> callBackDataMap){
        IOrderService orderService = SpringUtils.getBean(IOrderService.class);
        IOrderDoctorService orderDoctorService = SpringUtils.getBean(IOrderDoctorService.class);
        IApiService apiService = SpringUtils.getBean(IApiService.class);
        if(callBackDataMap.size()>0){
            Order order = orderService.selectOrderByProblemId(Integer.parseInt(callBackDataMap.get("service_id").toString()));
            Order fastPhoneNewOrder=new Order();
            BeanUtils.copyBeanProp(fastPhoneNewOrder,order);
            OrderDoctor od=null;

            OrderDoctor orderDoctor = orderDoctorService.selectOrderDoctorByProblemId(Integer.parseInt(callBackDataMap.get("service_id").toString()));
            if(StringUtils.isNull(orderDoctor)) {
                if(callBackDataMap.containsKey("doctor")){
                    Map<String,String > doctorMap = (Map<String,String>)callBackDataMap.get("doctor");
                    od=new OrderDoctor();
                    od.setId(UUID.randomUUID().toString().replaceAll("-",""));
                    od.setDoctorId(doctorMap.get("id"));
                    od.setProblemId(Integer.parseInt(callBackDataMap.get("service_id").toString()));
                    od.setPrice(fastPhoneNewOrder.getPrice());
                    od.setConsultType("TH");
                    od.setDoctorName(doctorMap.get("name").toString());
                    od.setInquiryTime(DateUtil.getStrFromDate(Calendar.getInstance().getTime(), ""));
                    od.setCallTime(Calendar.getInstance().getTime());
                    od.setProblemStatus(callBackDataMap.get("status").toString());
                    orderDoctorService.insertOrderDoctor(od);
                }
            }

            //正常结束
            if(callBackDataMap.get("status").equals("fast_phone_close")){
            }
            //订单退款
            if(callBackDataMap.get("status").equals("fast_phone_refund")){
                fastPhoneNewOrder.setPayStatus("T");
                SnowflakeIdWorker idWorker = new SnowflakeIdWorker(1, 2);
                fastPhoneNewOrder.setRefundOrderId(String.valueOf(idWorker.nextId()));
                fastPhoneNewOrder.setRefundPrice(fastPhoneNewOrder.getPrice());
                fastPhoneNewOrder.setRefundTime(Calendar.getInstance().getTime());

                /**退款参数组装 begin**/
                Map<String,String> refundParamsMap=new HashMap<>();
                refundParamsMap.put("transaction_id",fastPhoneNewOrder.getPayOrderId());
                refundParamsMap.put("out_trade_no",fastPhoneNewOrder.getId());
                refundParamsMap.put("out_refund_no",fastPhoneNewOrder.getRefundOrderId());
                refundParamsMap.put("total_fee",String.valueOf(fastPhoneNewOrder.getPrice()));
                refundParamsMap.put("refund_fee", String.valueOf(fastPhoneNewOrder.getPrice()));
                refundParamsMap.put("pay_type",fastPhoneNewOrder.getPayType());
                PaymentFeignClient paymentFeignClient = SpringUtils.getBean(PaymentFeignClient.class);
                /**退款参数组装 end**/



                /**推送订单退款消息 begin**/
                //调用退款接口
                if(paymentFeignClient.refund(JSONObject.toJSONString(refundParamsMap)).getSuccess()){
                    orderDoctor = orderDoctorService.selectOrderDoctorByProblemId(Integer.parseInt(callBackDataMap.get("service_id").toString()));
                    if(StringUtils.isNotNull(orderDoctor)) {
                        orderDoctor.setIsRefund(1);
                        orderDoctor.setRefundTime(Calendar.getInstance().getTime());
                        orderDoctor.setRefundNo(fastPhoneNewOrder.getRefundOrderId());
                        orderDoctorService.updateOrderDoctor(orderDoctor);
                    }
                    orderService.updateOrder(fastPhoneNewOrder);
                }
                /**推送订单退款消息 end**/
            }
        }
    }
}
